package cn.push.embed.core.http;

import android.content.Context;
import android.location.Location;

import http.com.zhy.http.okhttp.callback.Callback;

import java.util.HashMap;
import java.util.Map;

import cn.push.embed.config.API;
import cn.push.embed.core.CommData;
import cn.push.embed.core.utils.GetDeviceInfo;
import cn.push.embed.protocol.Utils;

/**
 * Created by shangdongzhou on 2017/3/27.
 */

public class UserModel {
    private Context mContext;

    public UserModel(Context mContext) {
        this.mContext = mContext;
    }

    /**
     * 手机重连时调用
     */
    public void server(String userId, Callback<String> callback) {
        Map<String, String> parameters = new HashMap<>();
        parameters.put("Version",API.Version);
        parameters.put("UserId", userId);
        parameters.put("AppKey", GetDeviceInfo.getMetaData(mContext, "PUSH_APPKEY"));
        parameters.put("Platform", "android");
        parameters.put("Identity", GetDeviceInfo.getIdentity(mContext));
        parameters.put("Devices", GetDeviceInfo.getDevices(mContext));
        Location location = GetDeviceInfo.getLocation(mContext);
        if (location != null) {
            parameters.put("Longitude", location.getLongitude() + "");
            parameters.put("Latitude", location.getLatitude() + "");
        }
        String sign = Utils.getMd5(GetDeviceInfo.getMetaData(mContext, "PUSH_APPKEY") + CommData.AppSecret + userId + GetDeviceInfo.getIdentity(mContext));
        parameters.put("Sign", sign);
        String Ip=GetDeviceInfo.getIPAddress(mContext);
        parameters.put("Ip",Ip);
        HttpBaseModel.post(API.SERVER, parameters, callback);

    }

    /**
     * 用户注册用户首次登录或清楚
     *
     * @param userId
     */
    public void register(String userId, Callback<String> callback) {
        Map<String, String> parameters = new HashMap<>();
        String Identity= GetDeviceInfo.getIdentity(mContext);
        parameters.put("Version",API.Version);
        parameters.put("UserId", userId);
        parameters.put("AppKey", GetDeviceInfo.getMetaData(mContext, "PUSH_APPKEY"));
        parameters.put("Platform", "android");
        parameters.put("Identity", Identity);
        parameters.put("Devices", GetDeviceInfo.getDevices(mContext));
        Location location = GetDeviceInfo.getLocation(mContext);
        if (location != null) {
            parameters.put("Longitude", location.getLongitude() + "");
            parameters.put("Latitude", location.getLatitude() + "");
        }
        String sign = Utils.getMd5(GetDeviceInfo.getMetaData(mContext, "PUSH_APPKEY") + CommData.AppSecret + userId + Identity);
        String Ip=GetDeviceInfo.getIPAddress(mContext);
        parameters.put("Ip",Ip);

        parameters.put("Sign", sign);
        HttpBaseModel.post(API.REGISTER, parameters, callback);
    }

    /**
     * 用户注册用户首次登录或清楚
     *
     * @param userId
     */
    public void logout(String userId, Callback<String> callback) {
        Map<String, String> parameters = new HashMap<>();
        parameters.put("Version",API.Version);
        parameters.put("UserId", userId);
        parameters.put("AppKey", GetDeviceInfo.getMetaData(mContext, "PUSH_APPKEY"));
        parameters.put("Platform", "android");
        parameters.put("Identity", GetDeviceInfo.getIdentity(mContext));
        parameters.put("Devices", GetDeviceInfo.getDevices(mContext));
        Location location = GetDeviceInfo.getLocation(mContext);
        if (location != null) {
            parameters.put("Longitude", location.getLongitude() + "");
            parameters.put("Latitude", location.getLatitude() + "");
        }
        String sign = Utils.getMd5(GetDeviceInfo.getMetaData(mContext, "PUSH_APPKEY") + CommData.AppSecret + userId + GetDeviceInfo.getIdentity(mContext));
        parameters.put("Sign", sign);
        HttpBaseModel.post(API.LOGOUT, parameters, callback);
    }

    /**
     * 首次联网
     * @param callback
     */
    public void device(Callback<String> callback){
        Map<String, String> parameters = new HashMap<>();
        parameters.put("Version",API.Version);
        parameters.put("AppKey", GetDeviceInfo.getMetaData(mContext, "PUSH_APPKEY"));
        parameters.put("Platform", "android");
        parameters.put("Identity", GetDeviceInfo.getIdentity(mContext));
        parameters.put("Devices", GetDeviceInfo.getDevices(mContext));
        Location location = GetDeviceInfo.getLocation(mContext);
        if (location != null) {
            parameters.put("Longitude", location.getLongitude() + "");
            parameters.put("Latitude", location.getLatitude() + "");
        }
        String Ip=GetDeviceInfo.getIPAddress(mContext);
        parameters.put("Ip",Ip);
        String sign = Utils.getMd5(GetDeviceInfo.getMetaData(mContext, "PUSH_APPKEY") + CommData.AppSecret  + GetDeviceInfo.getIdentity(mContext));
        parameters.put("Sign", sign);
        HttpBaseModel.post(API.DEVICE, parameters, callback);
    }




}
